package com.paper.tiger.easy.rpc.center.service;

import com.paper.tiger.easy.rpc.center.common.ServerMap;
import com.paper.tiger.easy.rpc.core.RegisterData;
import com.paper.tiger.easy.rpc.core.ServerType;
import com.paper.tiger.easy.rpc.core.Stub;
import io.netty.channel.ChannelHandlerContext;

import java.util.List;
import java.util.stream.Collectors;

/**
 * [简要描述]: 客户端查询请求
 * [详细描述]:
 *
 * @author ZouHaiBo
 * @version 1.0, 2021/6/15 08:51
 * @since JDK 1.8
 */
public class SelectRequestService implements BaseService
{

    @Override
    public void service(ChannelHandlerContext ctx,RegisterData data)
    {

        List<Stub> list = data.getList();

        ServerMap.focusServer(ctx,list);

        List<String> interfaceNames = list.stream().map(e -> e.getInterfaceClass()).collect(Collectors.toList());
        List<Stub> stubs = ServerMap.listByInterfaceNames(interfaceNames);
        if (stubs.isEmpty()){
            return;
        }
        data.setAppName(null);
        data.setList(stubs);
        data.setType(ServerType.SELECT_RESPONSE.getCode());
        ctx.channel().writeAndFlush(data);
    }

}
